/*******************************************************************************
*  file    : hg.cpp
*  created : 29.09.2012
*  author  : Slyshyk Oleksiy (alex312@meta.ua)
*******************************************************************************/

#include "hg.hpp"
#include "utils.hpp"

#include <QDebug>
#include <QCoreApplication>
#include <QTimer>
#include <QProcess>
#include <QTemporaryFile>
#include <QTextStream>
#include <QDateTime>

namespace hg
{

namespace internal
{
QString local_rev;
QString global_rev;
}

bool readHgRevisions()
{
    bool res = false;
    QProcess process;
    process.start("hg id -in");
    if (process.waitForStarted() && process.waitForReadyRead())
        {
            QByteArray ba = process.readAll();
            qDebug() << ba.data() ;
            QStringList lst = (QString(ba).replace("+","").replace("\n","")).split(" ");
            if(lst.size() >= 2)
                {
                    internal::global_rev = lst[0];
                    internal::local_rev  = lst[1];
                    res = true;
                }
            process.waitForFinished();
        }

    return res;
}

int writeFile(const QString& fileName,
              const QString& major,
              const QString& minor,
              const QString& buildNumber)
{
    // Create a temp file containing the version info and
    // only replace the existing one if they are different
    QTemporaryFile tempFile;
    if (tempFile.open())
        {
            QTextStream out(&tempFile);
            out << "#ifndef VERSION_H\r\n";
            out << "#define VERSION_H\r\n\r\n";
            out << "namespace Version\r\n";
            out << "{\r\n";
            out << "\tstatic const int  MAJOR = " << major << ";\r\n";
            out << "\tstatic const int  MINOR = " << minor << ";\r\n";
            out << "\tstatic const int  PATCH = " << buildNumber << ";\r\n\r\n";

            out << "\tstatic const int  LOCAL_REVISION    = " << internal::local_rev << ";\r\n";

            out << "\tinline const char* globalRevision()\r\n";
            out << "\t{\r\n";
            out << "\t\treturn \"" << internal::global_rev << "\";\r\n";
            out << "\t}\r\n\r\n";

            out << "\tinline const char* buildDate()\r\n";
            out << "\t{\r\n";
            out << "\t\treturn \"" << QDateTime::currentDateTime().toString("yyyy.MM.dd") << "\";\r\n";
            out << "\t}\r\n\r\n";

            out << "\tinline const char* buildTime()\r\n";
            out << "\t{\r\n";
            out << "\t\treturn \"" << QDateTime::currentDateTime().toString("hh:mm:ss") << "\";\r\n";
            out << "\t}\r\n\r\n";


            out << "\tinline const char* versionString()\r\n";
            out << "\t{\r\n";
            out << "\t\treturn \"" << major << '.' << minor << '.' << buildNumber << "\";\r\n";
            out << "\t}\r\n\r\n";


            out << "}\r\n\r\n";
            out << "#endif // VERSION_H\r\n";

            const QString tempFileName = tempFile.fileName();
            tempFile.close();

            if (!QFile::exists(fileName) || (utils::readFile(fileName) != utils::readFile(tempFileName)))
                {
                    QFile::remove(fileName);
                    QFile::copy(tempFileName, fileName);
                }
            QTimer::singleShot(0,qApp, SLOT(quit()) );
            return 0;
        }
    else
        {
            qDebug() << "Error creating temporary file!" ;
            QTimer::singleShot(0,qApp, SLOT(quit()) );
            return 1;
        }
}

}
